package com.rop.znyx.common.exception;

import com.rop.znyx.common.result.ResultCodeEnum;
import lombok.Data;

/**
 * @Title: BizException
 * @Author Rop
 * @Date 2023/6/7 21:24
 * @description: 业务异常
 */

@Data
public class BizException extends RuntimeException {

    /**
     * 异常状态码
     */
    private Integer code;

    /**
     * 通过状态码和错误消息创建异常对象
     */
    public BizException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    /**
     * 接收枚举类型对象
     */
    public BizException(ResultCodeEnum resultCodeEnum) {
        super(resultCodeEnum.getMessage());
        this.code = resultCodeEnum.getCode();
    }

    @Override
    public String toString() {
        return "BizException{" +
                "code=" + code +
                ", message=" + this.getMessage() +
                '}';
    }

    public static void error(ResultCodeEnum resultCodeEnum) {
        throw new BizException(resultCodeEnum);
    }

    public static void error(Exception e,Integer code) {
        throw new BizException(e.getMessage(), code);
    }

}
